import FView from "./FView";
import { IModule } from "../Interfaces/IModule";
import { getQualifiedClassName } from "../Utility/dx/getQualifiedClassName";
import { DelayFramer } from "../Ticks/DelayFramer";
import { Fun } from "../Utility/dx/Fun";
import { IDispose } from "../Interfaces/IDispose";

const {ccclass, property,menu} = cc._decorator;
@ccclass
@menu("Framework/基础组件/FViewModule")
export default class FViewModule extends FView implements IModule 
{
	public moduleName:string = null;
    /**是否需要对素材进行预加载 */
    public isNeedPreload:boolean = false;
    /**需要播放完动画才销毁 */
    public isPlayDisposeAnimation:boolean = false;

    private _isInitialize:boolean = false;// 模块是否初始化
    private delayCalls:any[] = [];
    protected moduleData:any = null;

    protected disObjects:IDispose[] = [];
    public get assets():any[]{return [];}
    public get isInitialize():boolean{return this._isInitialize;}
    public constructor()
    {
        super();
        this.moduleName = getQualifiedClassName(this);
    }
    public start():void{this.initViews();}
    public onLoad():void{this.startModule();}

    public startModule():void
    {
        if(this.isInitialize)return;
        this.initModule();
        this._isInitialize = true;
    }
    protected initModule():void
    {

    }

    
    protected initViews():void
    {
        this.createViews();
        this.showViews();
        DelayFramer.Push(Fun(this.excuteDelayCalls,this));
        
    }
    protected createViews():void
    {
    }
    protected showViews():void
    {
    }
    public excuteModuleFun(funName:string,...args):void
    {
        if(!this.isInitialize||this.delayCalls.length>0)this.delayCalls.push({funName:funName,args:args});
        else
        {
            let fun:Function = this[funName];
            if(fun!=null)fun.apply(this,args);
        }
    }
    private excuteDelayCalls():void
    {
        if(!this.isInitialize)return;
        while(this.delayCalls.length>0)
        {
            let o = this.delayCalls.shift();
            let fun:Function = this[o.funName];
            if(fun!=null)fun.apply(this,o.args);
        }
    }
    public show(p: cc.Node, data?: object): void 
    {
        this.moduleData = data;
        super.show(p,data);
        this.showViews();
    }

    protected addObject(obj:IDispose):void
    {
        if(this.disObjects.indexOf(obj)==-1)
            this.disObjects.push(obj);
    }
    protected disposeObjects():void
    {
        while(this.disObjects.length>0)
        {
            this.disObjects.shift().dispose();
        }
    }
    protected onHideAnimationComplete():any
    {
        this.isPlayDisposeAnimation = false;
        this.dispose();
    }
    public dispose():void
    {
        if(this.isPlayDisposeAnimation&&this.node&&this.node.isValid)
        {
            this.hide();
            if(this.node.parent)
            {
                let t:any[] = TweenLite.getTweensOf(this.node,true);
                if(t.length>0)
                    t[0].vars.onComplete = ()=>this.onHideAnimationComplete();
                return;// 如果Hide方法里面没有动效直接销毁模块
            }
        }
        this.disposeObjects();
        if(this.node&&this.node.isValid)
        {
            TweenLite.killTweensOf(this.node);
            this.node.destroy();
        }
        console.log(this.moduleName,"==>销毁完成！！！");
        this._isInitialize = false;
        this.delayCalls.length = 0;
        this.moduleData = null;
        this.moduleName = null;
    }
    // public onDestroy():void
    // {
    //     if(ModuleManager.instance.hasModule(this.moduleName))
    //         EventManager.dispatchModuleEventWith(ModuleEvent.DISPOSE_MODULE,this.moduleName);
    //     else this.dispose();
    //     // super.onDestroy();
    // }
}
